package cn.com.geovis.demo.handler;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

@Slf4j
@Data
public class TitleColorSheetWriteHandler extends AbstractCellStyleStrategy {

  //字体颜色
  private Short colorIndex = IndexedColors.BLACK.index;


  @Override
  protected void initCellStyle(Workbook workbook) {

  }

  @Override
  protected void setHeadCellStyle(Cell cell, Head head, Integer integer) {
    if (0 == cell.getRowIndex()) {
      // 根据单元格获取workbook
      Workbook workbook = cell.getSheet().getWorkbook();
      //设置行高
      cell.getSheet().getRow(cell.getRowIndex()).setHeight((short) (3 * 256));
      cell.getSheet().setColumnWidth(cell.getColumnIndex(), (short) (25 * 256));
      // 单元格策略
      WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
      // 设置背景颜色白色
      contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
      // 设置垂直居中为居中对齐
      contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
      // 设置左右对齐为靠左对齐
      contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER_SELECTION);
      // 设置单元格上下左右边框为细边框
      contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
      contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
      contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
      contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
      // 创建字体实例
      WriteFont cellWriteFont = new WriteFont();
      cellWriteFont.setFontName("楷体");
      cellWriteFont.setBold(false);
      // 设置字体大小
      cellWriteFont.setFontHeightInPoints((short) 16);
      cellWriteFont.setColor(colorIndex);
      contentWriteCellStyle.setWriteFont(cellWriteFont);
      CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, contentWriteCellStyle);
      cell.setCellStyle(cellStyle);
    }
  }

  @Override
  protected void setContentCellStyle(Cell cell, Head head, Integer integer) {

  }
}